package com.situ.b2c.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class IOUtil {
	
	public static void readAndWriter(InputStream inputStream,String toFilePath) {
		
		File toFile = new File(toFilePath);
		
		
		
		readAndWriter(inputStream, toFile);
		
		
	}
	public static void readAndWriter(InputStream inputStream,File toFile) {
		File parentFile = toFile.getParentFile();
		if (!parentFile.exists()) {
			parentFile.mkdirs();
		}
		OutputStream outputStream;
		try {
			outputStream = new FileOutputStream(toFile);
			readAndWriter(inputStream, outputStream);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
	}
	
	
	/**
	 * #完成读写
	 * @param inputStream
	 * @param outputStream
	 */
	public static void readAndWriter(InputStream inputStream,OutputStream outputStream) {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		
		bis = new BufferedInputStream(inputStream);
		bos = new BufferedOutputStream(outputStream);
		
		int length = -1;
		byte[] bytes = new byte[2048];
		try {
			while ((length=bis.read(bytes)) != -1) {
				bos.write(bytes,0,length);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (bos != null) {
					bos.close();
				}
				if (bis != null) {
					bis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
	}
}
